<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="学院" prop="collegesId">
        <college-selector v-model="form.collegesId"></college-selector>
      </el-form-item>
      <el-form-item label="学年" prop="schoolYear">
        <el-select v-model="form.schoolYear" placeholder="请选择学年">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.SCHOOL_YEAR)"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学期" prop="semester">
        <el-select v-model="form.semester" placeholder="请选择学期">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.COLLEGE_SEMESTER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="层级" prop="hierarchy">
        <el-select v-model="form.hierarchy" placeholder="请选择层级">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.STUDENT_TRAINING_LEVEL)"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="classesGrade">
        <el-select v-model="form.classesGrade" placeholder="请选择年级">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.CLSSS_PROPERTIES)"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程" prop="subjectIds">
        <el-select
          v-model="form.subjectIds"
          multiple
          placeholder="请选择课程"
          collapse-tags
        >
          <el-option
            v-for="dict in classList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教师" prop="teacherIds">
        <el-select
          v-model="form.teacherIds"
          multiple
          placeholder="请选择教师"
          collapse-tags
        >
          <el-option
            v-for="dict in teacherList"
            :key="dict.id"
            :label="dict.teacherName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          placeholder="请输入内容"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="parseInt(dict.value)"
            >{{ dict.label }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createPlan, updatePlan, getTeacherList, getClassList } from "@/api/study/plan";
import common from "@/api/collegebase/common";
import CollegeSelector from "@/components/CollegeSelector";

export default {
  name: "PlanEdit",
  components: {
    CollegeSelector
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        collegesId: [{ required: true, message: "学院不能为空", trigger: "blur" }],
        schoolYear: [{ required: true, message: "学年不能为空", trigger: "change" }],
        semester: [{ required: true, message: "学期不能为空", trigger: "change" }],
        hierarchy: [{ required: true, message: "层级不能为空", trigger: "change" }],
        subjectIds: [{ required: true, message: "课程不能为空", trigger: "change" }],
        teacherIds: [{ required: true, message: "教师不能为空", trigger: "change" }],
        classesGrade:  [{ required: true, message: "年级不能为空", trigger: "change" }],
      },
      teacherList: [],
      classList: [],
    };
  },
  watch: {
    "form.collegesId": {
      handler(newV, oldV) {
        this.teacherList = [];
        if (undefined != newV && newV != '') {
          this.getTeacherDataList(newV)
        }
      },
      immediate: true
    },
  },
  created() {
    this.getClassData();
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: ''
    }
  },
  methods: {

    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.datetimerange = [this.form.startTime, this.form.endTime]
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit('update:show', false)
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        collegesId: undefined,
        schoolYear: undefined,
        semester: undefined,
        hierarchy: undefined,
        classesGrade: undefined,
        remark: undefined,
        status: undefined,
        subjectIds: [],
        teacherIds: [],
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updatePlan(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit('success')
          });
          return;
        }
        // 添加的提交
        createPlan(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit('success')
        });
      });
    },
    //获取学生列表
    getTeacherDataList(collegeId) {
      getTeacherList({ collegeId }).then((response) => {
        this.teacherList = response.data;
      })
    },
    //获取所有课程
    getClassData() {
      getClassList({ findStr: '' }).then((response) => {
        this.classList = response.data;
      })
    }

  }
};
</script>
